package com.example.demo.controler;

import com.example.demo.entity.SolarTermEntity;
import com.example.demo.service.SolarTermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParser;
import org.springframework.boot.json.JsonParserFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("okhttp")
public class OkHttpController {
    @Autowired
    private ResourceLoader resourceLoader;

    @Autowired
    private SolarTermService solarTermService;

    @GetMapping("/hello")
    public String sayHello(){
        return "Hello World!";
    }
    @PostMapping("/testJson")
    public Map testJson(@RequestBody String json) {
        Map map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","成功");
        map.put("json",json);
        return map;
    }


    @PostMapping("/testForm")
    public Map testForm(@RequestParam("name") String name, @RequestParam("age") Integer age) {
       Map map = new HashMap<String,Object>();
       map.put("code",0);
       map.put("name",name);
       map.put("age",age);
       return map;
    }



    @GetMapping("/hello2")
    public Map sayHello2(){
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","成功");
        Map<String,Object> data = new HashMap<>();
        data.put("name","zhangsan");
        data.put("age",18);
        map.put("data",data);
        return map;
    }
    @GetMapping("/getnewlist")
    public Map<String, Object>  getNewList()  {
        Map<String, Object> datas = new HashMap<>();
        try {
            Resource resource = resourceLoader.getResource("classpath:static/data/newdata.json");
            InputStream fis = resource.getInputStream();
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            String line;
            StringBuffer sb= new StringBuffer();
            while((line = br.readLine()) != null) {
                sb.append(line);
            }
            br.close();
            isr.close();
            fis.close();
            JsonParser jsonParser = JsonParserFactory.getJsonParser();
            List<Object> list = jsonParser.parseList(sb.toString());
            datas.put("msg","成功");
            datas.put("code",0);
            datas.put("datas",list);

        }catch (Exception e){
            datas.put("msg",e.getMessage());
            datas.put("code","8888");
        }
        return datas;

    }

    @GetMapping("/getsolartermlist")
    public Map<String, Object>  getsolartermlist()  {
        List<SolarTermEntity> solarTermList = solarTermService.getSolarTermList();
        Map<String, Object> datas = new HashMap<>();
        datas.put("msg","成功");
        datas.put("code",0);
        datas.put("datas",solarTermList);
        return datas;

    }

}
